﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Web.Caching;
/// <summary>
///SystemConfig 的摘要说明
/// </summary>
public class SystemConfig
{
    // Fields
    private NameValueCollection _system = null;

    // Methods
    public void AddSystem(string strValue, string keys)
    {
        string filename = HttpContext.Current.Server.MapPath("/config/data.config");
        this.initSystemConfig();
        XmlDocument document = new XmlDocument();
        document.Load(filename);
        XmlNode node = document.SelectSingleNode("root");
        XmlNode node2 = node.SelectSingleNode("system");
        XmlNodeList list = node.SelectNodes("system");
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].SelectSingleNode(keys) != null)
            {
                XmlElement newChild = document.CreateElement(keys);
                newChild.InnerText = strValue;
                node2.AppendChild(newChild);
                document.Save(filename);
            }
        }
    }

    public void CreateSystemByNameCollection(NameValueCollection nvc)
    {
        if (nvc != null)
        {
            string filename = HttpContext.Current.Server.MapPath("/config/data.config");
            this.initSystemConfig();
            for (int i = 0; i < nvc.Count; i++)
            {
                string xpath = nvc.Keys[i];
                string str3 = nvc[xpath];
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                XmlNode node = document.SelectSingleNode("root");
                XmlNode node2 = node.SelectSingleNode("system");
                XmlNodeList list = node.SelectNodes("system");
                for (int j = 0; j < list.Count; j++)
                {
                    XmlNode node3 = list[j].SelectSingleNode(xpath);
                    if (node3 != null)
                    {
                        node3.InnerText = str3;
                        document.Save(filename);
                    }
                    else
                    {
                        XmlElement newChild = document.CreateElement(xpath);
                        newChild.InnerText = str3;
                        node2.AppendChild(newChild);
                        document.Save(filename);
                    }
                }
            }
        }
    }

    public NameValueCollection GetSystem()
    {
        if (this._system == null)
        {
            if (HttpContext.Current.Cache["system"] == null)
            {
                string filename = HttpContext.Current.Server.MapPath("/config/data.config");
                this.initSystemConfig();
                NameValueCollection values = new NameValueCollection();
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                XmlNodeList list = document.SelectNodes("/root/system/*");
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i] != null)
                        {
                            values.Add(list[i].Name, list[i].InnerText);
                        }
                    }
                }
                this._system = values;
                HttpContext.Current.Cache.Add("system", values, new CacheDependency(HttpContext.Current.Server.MapPath("/config/data.config")), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            }
            else
            {
                this._system = (NameValueCollection)HttpContext.Current.Cache["system"];
            }
        }
        return this._system;
    }

    private void initSystemConfig()
    {
        string path = HttpContext.Current.Server.MapPath("~/config/");
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        string fileName = HttpContext.Current.Server.MapPath("/config/data.config");
        FileInfo info = new FileInfo(fileName);
        if (!info.Exists)
        {
            info.Create().Close();
            StreamWriter writer = File.AppendText(fileName);
            writer.WriteLine("<?xml version=\"1.0\"?><root><system></system></root>");
            writer.Close();
        }
    }

    public void UpdateSystem(string strValue, string keys)
    {
        string filename = HttpContext.Current.Server.MapPath("/config/data.config");
        this.initSystemConfig();
        XmlDocument document = new XmlDocument();
        document.Load(filename);
        XmlNode node = document.SelectSingleNode("root");
        XmlNode node2 = node.SelectSingleNode("system");
        XmlNodeList list = node.SelectNodes("system");
        for (int i = 0; i < list.Count; i++)
        {
            XmlNode node3 = list[i].SelectSingleNode(keys);
            if (node3 != null)
            {
                node3.InnerText = strValue;
                document.Save(filename);
            }
        }
    }
}

